import { createRouter, createWebHashHistory } from "vue-router"

// 2. 定义一些路由
// 每个路由都需要映射到一个组件。可以有嵌套子路由。
const routes = [{
		path: '/',
		name: 'home',
		component: () => import('@/views/home/index.vue'),
		meta: {
			title: "首页"
		}
	},
	// 商品列表
	{
		path: '/productList',
		name: "productList",
		component: () => import('@/views/product/productList.vue'),
		meta: {
			title: "商品"
		}
	},
	// 商品详情 
	{
		path: '/productDetail',
		name: "productDetail",
		component: () => import('@/views/product/productDetail.vue')
	},

	//购物车
	{
		path: '/shopcart',
		name: "shopcart",
		component: () => import('@/views/shopcart/index.vue'),
		meta: {
			title: "购物车",
			requireAuth: true, //需要Token
		}
	},

	//我的页面
	{
		path: '/user',
		name: "user",
		component: () => import('@/views/user/index.vue'),
		meta: {
			title: "我的"
		}
	},
	//登录
	{
		path: "/login",
		component: () => import("@/views/user/login.vue"),
		name: "login",
		meta: {
			title: "登录"
		}
	},
	//注册
	{
		path: "/register",
		component: () => import("@/views/user/register.vue"),
		name: "register",
		meta: {
			title: "用户注册"
		}
	},
	//个人资料
	{
		path: "/ruser/profile",
		component: () => import("@/views/user/profile.vue"),
		name: "profile",
		meta: {
			title: "个人资料"
		}
	},
	// 修改密码
	{
		path: "/user/updatePwd",
		component: () => import("@/views/user/updatePwd.vue"),
		name: "updatePwd",
		meta: {
			title: "修改密码"
		}
	},

	// 新建订单
	{
		path: "/order/create",
		component: () => import("@/views/order/orderCreate.vue"),
		name: "orderCreate",
		meta: {
			title: "新建订单",
		},
	},
	//我的订单列表
	{
		path: "/order/list",
		component: () => import("@/views/order/orderList.vue"),
		name: "orderList",
		meta: {
			title: "我的订单",
			requireAuth: true
		},
	},
	//订单详情
	{
		path: "/order/detail",
		component: () => import("@/views/order/orderDetail.vue"),
		props: true,
		name: "orderDetail",
		meta: {
			title: "订单详情",
			requireAuth: true
		},
	},



	//收货地址-列表
	{
		path: "/address/addressList",
		component: () => import("@/views/address/addressList.vue"),
		name: "addressList",
		meta: {
			title: "收货地址-列表",
			requireAuth: true
		}
	},
	//收货地址-编辑
	{
		path: "/address/addressEdit",
		component: () => import("@/views/address/addressEdit.vue"),
		name: "addressEdit",
		meta: {
			title: "收货地址-编辑",
			requireAuth: true
		},
	},

	//新闻列表
	{
		path: "/news/list",
		component: () => import("@/views/news/index.vue"),
		name: "newsList",
		meta: {
			title: "新闻列表",
		},
	},

	{
		path: "/working",
		component: () => import("@/views/working.vue"),
		name: "working",
		meta: {
			title: "努力中...."
		},
	},
	{
		path: "/:pathMatch(.*)",
		name: "notFound",
		component: () => import('@/views/notFound.vue'),
		meta: {
			title: "导航页面不存在"
		},
	},
	{
		path: "/images",
		name: "images",
		component: () => import('@/views/images/images.vue'),
		meta: {
			title: "图片分享"
		},
	},
	{
		path: "/message",
		name: "message",
		component: () => import('@/views/message/message.vue'),
		meta: {
			title: "留言反馈"
		},
	},
	{
		path: "/contact",
		name: "contact",
		component: () => import('@/views/contact/contact.vue'),
		meta: {
			title: "联系我们"
		},
	},
	
]

// 3. 创建路由实例并传递 `routes` 配置
const router = createRouter({
	// 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
	history: createWebHashHistory(),
	routes, // `routes: routes` 的缩写
})

import { showConfirmDialog } from 'vant';
import { getTokenLocal } from "@/utils/auth.js"
router.beforeEach((to, from, next) => {
	if (to.name === 'login') { // 路由条状到login的，直接放行
		console.log(`${from.name}跳转到login`); // 控制台调试输出
		next(); // 路由继续往下跳转
		return;
	}

	if (!to.meta.requireAuth) { // 路由不需要验证的，直接放行
		console.log(`${from.name}跳转到${to.name},目的路由不需要用户认证`); // 控制台调试输出
		next();
		return;
	}

	// 路由需要验证的   
	const token = getTokenLocal();
	if (!token) { // 如果token不存在的，则跳转登录login
		console.log(`${from.name}跳转到${to.name},目的路由需要用户认证，Token不存在！强制跳转到login`); // 控制台调试输出
		showConfirmDialog({
			message: "没有Token,需要重新登录吗?",
		}).then(() => {
			// 需要登录，则跳转到登录login，并传递参数(登录后跳转的路由)
			next({
				name: 'login',
				query: { redirect: to.path }
			});
		}).catch(() => {
			// 不需要登录，跳转到首页            
			router.push({ name: "home" });
		});
		return;
	}
	console.log(`${from.name}跳转到${to.name},目的路由需要用户认证，Token也存在！`); // 控制台调试输出
	// 其余情况，则路由放行
	next(); //
});

export default router